//index.js
//获取应用实例
const app = getApp()
const host = app.globalData.host;
var pageModel = null;

function loadBeacons(callback) {
  pageModel.setData({
    locations: []
  })
  wx.getBeacons({
    success(res) {
      callback(res.beacons)
    }
  })
  setTimeout(function () {
    loadBeacons(callback)
  }, 5000)
}
function getUuid() {
  app.request({
    url: host + '/explore/uuid',
    data: {
    },
    complete: function (res) {
      console.warn(res)
    },
    success: function (res) {
      var info = res.data.info
      var temp = []
      for (var index in info) {
        temp.push(info[index].uuid)
      }
      pageModel.setData({
        uuids: temp
      })
      startIbeacon()
    },
    fail: function (res) {
      console.warn(res)
    }
  })

}
function startIbeacon() {

  wx.startBeaconDiscovery({
    uuids: pageModel.data.uuids,
    success: function (res1) {
      // console.log("startBeaconDiscovery…" + res1);
      // console.log("startBeaconDiscovery…" + res1.errMsg);
      // wx.onBeaconUpdate(function (res) {
      //   // console.log(res.beacons)
      // });
    },
    fail: function (res) {
      wx.showModal({
        title: '蓝牙打开失败',
        content: '',
      })
      console.log("onBeaconUpdate…fail…." + res.errMsg);
    }
  });
}

function getList(value) {
  console.log(value)
}
Page({
  data: {
    locationArr: [],
    locations: [],
    advs: [],
    uuids: [],
    beaconArray: [],
    test:'',
  },
  //事件处理函数
  onPullDownRefresh: function () {
    var self = this;
    self.getLocations()
  },
  pushInfo(e) {
    var href = e.currentTarget.dataset.url;
    wx.navigateTo({
      url: '/pages/info/info?href=' + href,
    })
  },
  getLocations() {
    var self = this;
    app.request({
      url: host + '/explore/location',
      data: {
      },
      complete: function (res) {
        console.warn(res)
      },
      success: function (res) {
        self.setData({
          locationArr: res.data.info
        })
      },
      fail: function (res) {
        console.warn(res)
      }
    })
  },
  getAdvs() {
    var self = this;
    app.request({
      url: host + '/explore/adv',
      data: {
      },
      complete: function (res) {
        console.warn(res)
      },
      success: function (res) {
        self.setData({
          advs: res.data.info
        })
      },
      fail: function (res) {
        console.warn(res)
      }
    })
  },
  onLoad: function () {
    var self = this;
    pageModel = this;
    self.getLocations()
    self.getAdvs()
    if (app.globalData.userInfo) {
      this.setData({
        userInfo: app.globalData.userInfo,
        hasUserInfo: true
      })
    } else if (this.data.canIUse) {
      // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
      // 所以此处加入 callback 以防止这种情况
      app.userInfoReadyCallback = res => {
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    } else {
      // 在没有 open-type=getUserInfo 版本的兼容处理
      wx.getUserInfo({
        success: res => {
          app.globalData.userInfo = res.userInfo
          this.setData({
            userInfo: res.userInfo,
            hasUserInfo: true
          })
        }
      })
    }
    loadBeacons(function (res) {
      if (res.length == 0) {
        pageModel.setData({
          locations: []
        })
        return false;
      }
      var beaconArray = res
      var locationArr = pageModel.data.locationArr
      var tempLocations = []
      for (var i = 0; i < beaconArray.length; i++) {
        var accuracyI = parseInt(beaconArray[i].accuracy)
        for (var j = i + 1; j < beaconArray.length; j++) {
          var accuracyJ = parseInt(beaconArray[j].accuracy)
          if (accuracyI > accuracyJ) {
            var tmp = beaconArray[i];
            beaconArray[i] = beaconArray[j];
            beaconArray[j] = tmp;
          }
        }
      }
      for (var i in beaconArray) {
        for (var j in locationArr) {
          if (beaconArray[i].accuracy == -1) {
            break;
          }
          if (beaconArray[i].major == locationArr[j].major) {
            var accuracy = parseInt(beaconArray[i].accuracy)
            if (accuracy <= 10) {
              accuracy = 10
            } else if (accuracy <= 50) {
              accuracy = 50
            } else {
              accuracy = 100
            }
            locationArr[j].accuracy = accuracy
            tempLocations.push(locationArr[j])
          }
        }
      }
      pageModel.setData({
        locations: tempLocations
      })
    });
    getUuid()
  },
  getUserInfo: function (e) {
    app.globalData.userInfo = e.detail.userInfo
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
  }
})
